package com.bgs.microservices.auth.sysuser.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgs.microservices.auth.sysuser.entity.SysUser;
import com.bgs.microservices.auth.sysuser.mapper.SysUserMapper;
import com.bgs.microservices.auth.sysuser.service.SysUserService;
import com.bgs.microservices.auth.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


/**
 * @ClassName SysUserServiceImpl
 * @Description 用户管理服务类 用于实现用户的增删改查 登录 注销
 * @Author zcy
 * @Date 2023/3/24 16:15
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 通过登录名获取用户信息
     * @param loginName
     * @return
     */
    @Override
    public SysUser getUserByLoginName(String loginName) {
        // 判断用户登录名是否为空
        if(ObjectUtils.isNotEmpty(loginName)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("login_name",loginName);
            return  sysUserMapper.selectOne(wrapper);
        }else{
            return null;
        }
    }
    /**
     * 通过邮箱查询用户
     * @param emailAccount
     * @return
     */
    @Override
    public SysUser getUserByEmailAccount(String emailAccount) {
        // 判断用户登录名是否为空
        if(ObjectUtils.isNotEmpty(emailAccount)){
            // 使用QueryWrapper表达式查询登录人信息
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("email",emailAccount);
            return  sysUserMapper.selectOne(wrapper);
        }else{
            return null;
        }
    }

    /**
     * 更新登录成功后更新用户信息
     * @param sysUser
     */
    @Override
    public void updateUserByLogin(SysUser sysUser) {
        if(ObjectUtils.isNotEmpty(sysUser)&&ObjectUtils.isNotEmpty(sysUser.getId())){
            sysUserMapper.updateById(sysUser);
        }
    }

    /**
     * 根据手机号查询用户信息
     * @param phoneAccount
     * @return
     */
    @Override
    public SysUser getUserByPhoneAccount(String phoneAccount) {
        // 判断用户登录名是否为空
        if(ObjectUtils.isNotEmpty(phoneAccount)){
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("phone",phoneAccount);
            return sysUserMapper.selectOne(queryWrapper);
        }else{
            return null;
        }
    }
}
